package ar.edu.unicen.pladema.ui.model;

import org.eclipse.swt.graphics.RGB;

public class ColorRange {
	private int index;
	private RGB color;
	private ColorRange next = null;
	private ColorProgress cp = null;
	
	public ColorRange(int i, RGB color) {
		this.index = i;
		this.color = color;
	}
	
	public ColorRange add(int i, RGB color) {
		if (i > this.index) {
			if (this.next == null) {
				ColorRange n = new ColorRange(i, color);
				this.next = n;
				return this;
			}
			this.next = this.next.add(i, color);
			return this;
		}
		ColorRange n = new ColorRange(i, color);
		n.setNext(this);
		return n;
	}
	public void setNext(ColorRange n) {
		this.next = n;
	}
	public ColorRange getNext() {
		return next;
	}
	public RGB get(int i) {
		if (i == this.getIndex()) {
			return this.getColor();
		}
		if (this.next == null) {
			System.out.println(i + " ColorRange next is null:" + index);
			return this.getColor();
		}
		if (i < this.next.getIndex()) {
			return this.getColorProgress().getColorAt(this.next.getIndex(), this.getIndex(), i);
		}
		return this.next.get(i);
	}

	private ColorProgress getColorProgress() {
		if (cp == null) {
			cp = new ColorProgress(this.getColor(), this.next.getColor());
		}
		return cp;
	}

	public RGB getColor() {
		return this.color;
	}

	public int getIndex() {
		return index;
	}

	public void printList() {
		System.out.println(index + ":" + color.toString());
		if (this.next == null) return;
		this.next.printList();
	}
}
